/*
 * app_hub_api_event_link.c
 *
 *  Created on: 2023年7月24日
 *      Author: HXF
 */
#include "app_hub_api_def.h"
#include "app_hub_api_event_link.h"
#include "em_core.h"
#include "string.h"
#include "stdlib.h"
#include "app_log.h"
#include "link.h"
#include "device.h"

static link_t *usart_rx_event_link_head = NULL;
static link_t *usart_tx_event_link_head = NULL;
//static link_t *rf_rx_event_link_head = NULL;
//static link_t *rf_tx_event_link_head = NULL;

static int usart_rx_node_count = 0;
static int usart_tx_node_count = 0;  //做队列拥堵预警

void DS_app_hub_api_event_link_init(void)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  usart_rx_event_link_head = link_creathead();
  usart_tx_event_link_head = link_creathead();
  //rf_rx_event_link_head = link_creathead();
  //rf_tx_event_link_head = link_creathead();
  usart_rx_node_count = 0;
  usart_tx_node_count = 0;
  CORE_EXIT_CRITICAL();
}

bool DS_app_hub_api_usart_rx_event_link_is_empty(void)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(!usart_rx_event_link_head){
      bret = true;
  }else{
      if(!usart_rx_event_link_head->next){
          bret = true;
      }
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int DS_app_hub_api_usart_rx_event_link_push(void * msg, int len)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  iret = link_push_end(usart_rx_event_link_head, msg, len); //
  CORE_EXIT_CRITICAL();
  return iret;
}

int DS_app_hub_api_usart_rx_event_link_pop(void * msg, int * ptr_len)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  iret = link_pop(usart_rx_event_link_head, msg, ptr_len);
  CORE_EXIT_CRITICAL();
  return iret;
}

bool DS_app_hub_api_usart_tx_event_link_is_empty(void)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(!usart_tx_event_link_head){
      bret = true;
  }else{
      if(!usart_tx_event_link_head->next){
          bret = true;
      }
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int DS_app_hub_api_usart_tx_event_link_push(void * msg, int len)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  unsigned char enable_retrans = 0;
  iret = link_push_end(usart_tx_event_link_head, msg, len);
  if(iret == 0){
      usart_tx_node_count++;
      if(usart_tx_node_count > 40){
          setThisParam(ENbyEnableRetrans, (unsigned char *)&enable_retrans); //失能重传
          APP_PRINTF("t-i too many: %d\r\n",usart_tx_node_count);
      }
      if(hub_log_enable){
          APP_PRINTF("t-i: %d\r\n",usart_tx_node_count);
      }
  }
  CORE_EXIT_CRITICAL();
  return iret;
}

int DS_app_hub_api_usart_tx_event_link_re_push(void * msg, int len)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  unsigned char enable_retrans = 0;
  iret = link_push_head(usart_tx_event_link_head, msg, len);
  if(iret == 0){
      usart_tx_node_count++;
      if(usart_tx_node_count > 40){
          setThisParam(ENbyEnableRetrans, (unsigned char *)&enable_retrans); //失能重传
      }
      if(hub_log_enable){
          APP_PRINTF("t-i: %d\r\n",usart_tx_node_count);
      }
  }
  CORE_EXIT_CRITICAL();
  return iret;
}

int DS_app_hub_api_usart_tx_event_link_pop(void * msg, int * ptr_len)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  unsigned char enable_retrans = 1;
  iret = link_pop(usart_tx_event_link_head, msg, ptr_len);
  if(iret == 0){
      if(usart_tx_node_count){
          usart_tx_node_count--;
      }
      if(!usart_tx_node_count){ //链表空
          setThisParam(ENbyEnableRetrans, (unsigned char *)&enable_retrans); //使能重传
      }
      if(hub_log_enable){
          APP_PRINTF("t-o: %d\r\n",usart_tx_node_count);
      }
  }
  CORE_EXIT_CRITICAL();
  return iret;
}
#if(0)
bool DS_app_hub_api_rf_rx_event_link_is_empty(void)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(!rf_rx_event_link_head){
      bret = true;
  }else{
      if(!rf_rx_event_link_head->next){
          bret = true;
      }
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int DS_app_hub_api_rf_rx_event_link_push(hub_api_rx_evt_t *evt)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  CORE_EXIT_CRITICAL();
  return iret;
}

int DS_app_hub_api_rf_rx_event_link_pop(hub_api_rx_evt_t *evt)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  CORE_EXIT_CRITICAL();
  return iret;
}

bool DS_app_hub_api_rf_tx_event_link_is_empty(void)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(!rf_tx_event_link_head){
      bret = true;
  }else{
      if(!rf_tx_event_link_head->next){
          bret = true;
      }
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int DS_app_hub_api_rf_tx_event_link_push(hub_api_tx_evt_t *evt)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  CORE_EXIT_CRITICAL();
  return iret;
}

int DS_app_hub_api_rf_tx_event_link_pop(hub_api_tx_evt_t *evt)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  int iret = 0;
  CORE_EXIT_CRITICAL();
  return iret;
}
#endif
